import 'package:fish_redux/fish_redux.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:oktoast/oktoast.dart';
import 'package:school_safe_teacher/base/entity_factory.dart';
import 'package:school_safe_teacher/constans/all_info.dart';
import 'package:school_safe_teacher/constans/constants.dart';
import 'package:school_safe_teacher/me/entity/userEntity.dart';
import 'package:school_safe_teacher/me/page.dart';
import 'package:school_safe_teacher/network/dio_api.dart';
import 'package:school_safe_teacher/network/http_api.dart';
import 'package:school_safe_teacher/utils/sp_utils.dart';

import 'action.dart';
import 'state.dart';

Effect<MeState> buildEffect() {
  return combineEffects(<Object, Effect<MeState>>{
    Lifecycle.initState:_init,

  });
}

void _init(Action action, Context<MeState> ctx) {
  _requstUserInfo(ctx);
}
Future<void> _requstUserInfo(Context<MeState> ctx) async {
  Map<String, String> params = Map();
  params["class_id"] = await SpUtil.getStringItem(Constants.classID);
  DioApi.instance.asyncRequestNetwork<UserEntity>(
      Method.post, HttpApi.USER_INFO, ctx.context,params: params, onStart: () {
    EasyLoading.show();
  },
      onSuccess:(dynamic t){
        UserEntity entity= EntityFactory.generateOBJ(t);
        AllInfo.instance.setUserEntity(entity);
        ctx.state..userEntity = entity;
        ctx.dispatch(MeActionCreator.onAction());
      }, onError: (String msg) {
    showToast(msg);
  }, onDone: () {
    EasyLoading.dismiss();
  }, isList: false);
}